//
//  PopoverAnimator.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/9/22.
//

import UIKit

// 弹出消失动画封装类

class PopoverAnimator: NSObject {
    // MARK: - 属性
    var isPresented : Bool = false
    var presentedFrame : CGRect = .zero
    
    // a.闭包,回传监听事件
    var callBack : ((_ presentd : Bool) -> ())?
    
    // MARK: - 自定义构造函数 c.
    // 注意:如果自定义一个构造函数,没有对默认构造函数init()进行重写,那么自定义的构造函数会覆盖默认的init()构造函数
//    override init() {
//        super.init()
//    }
    
    init(callBack : @escaping (_ presentd : Bool) -> ()) {
        self.callBack = callBack
    }
    
}

// MARK: - 自定义转厂代理方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    // 目的:改变弹出View的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = WBPresentationController(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    
    // 目的:自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        // b.调用
        isPresented = true
        callBack!(isPresented)
        
        return self
    }
    
    // 目的:自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        callBack!(isPresented)
        
        return self
    }
}

// MARK: - 弹出和消失动画代理的方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    // 动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)
    }
    
    // 自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        /*
             获取转场的上下文:可以通过转场上下文获取弹出的View和消失的View
             UITransitionContextViewKey.from:获取消失的View
             UITransitionContextViewKey.to:获取弹出的View
         */
        
        // 1.获取弹出的View
        let presentedView = transitionContext.view(forKey: .to)!
        
        // 2.将弹出的View添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        // 3.执行动画
        presentedView.transform = CGAffineTransform(scaleX: 1, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
            presentedView.transform = CGAffineTransform.identity
        } completion: { _ in
            //  必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    
    // 自定义消失动画
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取消失的View
        let dismissView = transitionContext.view(forKey: .from)
        
        // 2.执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
        } completion: { _ in
            dismissView?.removeFromSuperview()
            //  必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
}
